/**
 * Created by Administrator on 2018/1/27.
 */
let mem = require('../util/mem.js');
let msg = require('../util/msg.js');
let arenaA = require('./arenaModeA.js');
let userDao = require('../mysql/userDao.js');
var util = require('../util/util.js');

let method = module.exports;

method.enter = function (obj, cid, data) {
    switch (data.mode) {
        case 1:
        case "1":
            method.enterModeA(obj, cid, data);
            break;
    }
};
method.matchIn = function (obj, cid, data) {
    if (!mem.arenaPlayers[obj.userId]) {
        msg.send(cid, obj.uuid, 'error', {'code': 10085});
    }
    switch (mem.arenaPlayers[obj.userId].mode) {
        case 1:
            arenaA.getInstance().matchIn(obj.userId);
            msg.send(cid, obj.uuid, 'arenaMatchIn');
            break;
    }
};
method.matchOut = function (obj, cid, data) {
    if (!mem.arenaPlayers[obj.userId]) {
        msg.send(cid, obj.uuid, 'error', {'code': 10085});
    }
    switch (mem.arenaPlayers[obj.userId].mode) {
        case 1:
            arenaA.getInstance().matchOut(obj.userId);
            msg.send(cid, obj.uuid, 'arenaMatchOut');
            break;
    }
};
method.enterModeA = function (obj, cid, data) {
    if (!arenaA.getInstance()._open) {
        msg.send(cid, obj.uuid, 'error', {'code': 10086});
    }
    if (!arenaA.getInstance().inPlayer(obj.userId)) {
        let userModel = new userDao();
        userModel.inId(obj.userId, function (err, res) {
            if (res == null) {
                return false;
            }
            if (res.res.gameId && res.res.gameId != 0) {
                msg.send(cid, uuid, 'matchInGame', {
                    'userId': res.res.id,
                    'gameId': res.res.gameId
                });
                return false;
            }
            if (res.res.gold < 10) {
                msg.send(cid, obj.uuid, 'error', {'code': 10087});
                return false;
            }
            let resultData = {};
            userModel.add('gold', -10);
            resultData.gold = {'now': userModel.data.gold, 'add': -10};
            userModel.save(function (err1, res1) {
                if (res1 == null) {
                    return false;
                }
                msg.send(cid, obj.uuid, 'globalUserChange', resultData);
            });
            let data = util.userInfo(userModel.data);
            data.cid = cid;
            data.uuid = obj.uuid;
            delete data.gold;
            delete data.score;
            delete data.gem;
            arenaA.getInstance().join(data);
            arenaA.getInstance().joinArena(data.id);
            mem.arenaPlayers[data.id] = {'mode': 1};
            msg.send(cid, obj.uuid, 'arenaEnter', arenaA.getInstance().data());
        });
    } else {
        msg.send(cid, obj.uuid, 'arenaEnter', arenaA.getInstance().data());
    }
};
method.rpcInGame = function (obj, cid, data) {
    switch (data.action) {
        case 'gameOverArena':
            switch (data.mode) {
                case 1:
                    arenaA.getInstance().gameOver(data);
                    break;
            }
            break;
    }
};
